package com.quectel.core.module.schedulingapply.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.schedulingapply.dao.SchedulingApplyRecordAuditPersonDao;
import com.quectel.core.module.schedulingapply.dto.SchedulingApplyRecordAuditPersonDto;
import com.quectel.core.module.schedulingapply.entity.SchedulingApplyRecordAuditPersonEntity;
import com.quectel.core.module.schedulingapply.service.SchedulingApplyRecordAuditPersonService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/13 19:14
 */
@DubboService
public class SchedulingApplyRecordAuditPersonServiceImpl extends ServiceImpl<SchedulingApplyRecordAuditPersonDao, SchedulingApplyRecordAuditPersonEntity> implements SchedulingApplyRecordAuditPersonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingApplyRecordAuditPersonServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_APPLY_RECORD_AUDIT_PERSON_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_APPLY_RECORD_AUDIT_PERSON_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingApplyRecordAuditPersonDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_APPLY_RECORD_AUDIT_PERSON_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SchedulingApplyRecordAuditPersonDao schedulingApplyRecordAuditPersonDao;
    @Autowired
    private DeptPersonService deptPersonService;

    @Override
    public List<SchedulingApplyRecordAuditPersonDto> selectByRecordId(Long recordId) {
        List<SchedulingApplyRecordAuditPersonEntity> list = schedulingApplyRecordAuditPersonDao.selectList(
                new LambdaQueryWrapper<SchedulingApplyRecordAuditPersonEntity>()
                        .eq(SchedulingApplyRecordAuditPersonEntity::getApplyRecordId, recordId)
        );
        List<SchedulingApplyRecordAuditPersonDto> result = CopyUtils.copyList(list, SchedulingApplyRecordAuditPersonDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingApplyRecordAuditPersonDto> selectByRecordAndSeq(Long recordId, Integer seq) {
        List<SchedulingApplyRecordAuditPersonEntity> list = schedulingApplyRecordAuditPersonDao.selectList(
                new LambdaQueryWrapper<SchedulingApplyRecordAuditPersonEntity>()
                        .eq(SchedulingApplyRecordAuditPersonEntity::getApplyRecordId, recordId)
                        .eq(SchedulingApplyRecordAuditPersonEntity::getCurrentAtSeq, seq)
        );
        List<SchedulingApplyRecordAuditPersonDto> result = CopyUtils.copyList(list, SchedulingApplyRecordAuditPersonDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public void saveBatch(List<SchedulingApplyRecordAuditPersonDto> applyRecordAuditPersonDtos) {
        List<SchedulingApplyRecordAuditPersonEntity> list = CopyUtils.copyList(applyRecordAuditPersonDtos, SchedulingApplyRecordAuditPersonEntity.class);
        for (SchedulingApplyRecordAuditPersonEntity entity : list) {
            entity.setId(Snowflake.nextId());
        }
        super.saveBatch(list);
    }

    @Override
    public SchedulingApplyRecordAuditPersonDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_APPLY_RECORD_AUDIT_PERSON_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingApplyRecordAuditPersonDto selectById(Long id) {
        SchedulingApplyRecordAuditPersonEntity entity = schedulingApplyRecordAuditPersonDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, SchedulingApplyRecordAuditPersonDto.class));
    }

    @Override
    public List<SchedulingApplyRecordAuditPersonDto> queryList(Map<String, Object> params) {
        List<SchedulingApplyRecordAuditPersonEntity> list = schedulingApplyRecordAuditPersonDao.queryList(params);
        List<SchedulingApplyRecordAuditPersonDto> result = CopyUtils.copyList(list, SchedulingApplyRecordAuditPersonDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingApplyRecordAuditPersonDto> queryListWithoutPadding(Map<String, Object> params) {
        List<SchedulingApplyRecordAuditPersonEntity> list = schedulingApplyRecordAuditPersonDao.queryList(params);
        return CopyUtils.copyList(list, SchedulingApplyRecordAuditPersonDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingApplyRecordAuditPersonDto paddingField(SchedulingApplyRecordAuditPersonDto dto) {
        if (dto != null) {
            DeptPersonDto deptPersonDto = deptPersonService.selectCacheById(dto.getAuditDeptPersonId());
            if (deptPersonDto != null) {
                dto.setDeptPersonName(deptPersonDto.getName());
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return schedulingApplyRecordAuditPersonDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingApplyRecordAuditPersonDto dto) {
        SchedulingApplyRecordAuditPersonEntity entity = CopyUtils.copyObj(dto, SchedulingApplyRecordAuditPersonEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingApplyRecordAuditPersonDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingApplyRecordAuditPersonDto dto) {
        SchedulingApplyRecordAuditPersonEntity entity = CopyUtils.copyObj(dto, SchedulingApplyRecordAuditPersonEntity.class);

        schedulingApplyRecordAuditPersonDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingApplyRecordAuditPersonDto dto) {
        SchedulingApplyRecordAuditPersonEntity entity = CopyUtils.copyObj(dto, SchedulingApplyRecordAuditPersonEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, schedulingApplyRecordAuditPersonDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingApplyRecordAuditPersonDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
